package com.seeyon.apps.attendance.util;

import cn.hutool.core.convert.Convert;
import com.seeyon.cap4.form.bean.*;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.util.DateUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Description
 * <pre></pre>
 * @author FanGaowei<br>
 * Copyright(c) Beijing Seeyon Software Co.,LTD
 */
public class CAP4FormKit {

    private static EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");

	/**
	 * 根据表单的显示名称获取字段的值
	 * @param bean
	 * @param disPlay
	 * @return
	 */
	public static Object getFieldValue(FormDataBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		if(field == null) {
			return null;
		}
		return bean.getFieldValue(field.getName());
	}

	public static Long getLongValue(FormDataBean bean, String disPlay) {
		Object value = getFieldValue(bean, disPlay);
		if(null == value) {
			return null;
		}
		if(value instanceof Long) {
			return (Long) value;
		} else if(value instanceof BigDecimal) {
			return ((BigDecimal) value).longValue();
		} else {
			return Long.valueOf(value.toString());
		}
	}

	public static Date getDateValue(FormDataBean bean, String disPlay) {
		Object value = getFieldValue(bean, disPlay);
		if(null == value) {
			return null;
		}
		return (Date) value;
	}

	public static String getEnumCode(FormDataBean bean, String disPlay) {
		Long value = getLongValue(bean, disPlay);
		if(null == value) {
			return null;
		}
		CtpEnumItem item = enumManager.getEnumItem(value);
		if(null != item) {
			return item.getEnumItemCode();
		}
		return "";
	}

	public static String getFormatDateValue(FormDataBean bean, String disPlay, String format) {
		Date value = getDateValue(bean, disPlay);
		if(null == value) {
			return null;
		}
		return DateUtil.format(value, format);
	}

	public static Object getFieldValueByName(FormDataBean bean, String fieldName) {
        return bean.getFieldValue(fieldName);
    }

	/**
	 * 根据表单的显示名称获取字段是  field000？
	 * @param bean
	 * @param disPlay
	 * @return
	 */
	public static String getFieldTaleId(FormBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getMasterTableBean();
		return getFieldTaleId(table, disPlay);
	}

	/**
	 * 直接根据table来获取
	 * @param table
	 * @param disPlay
	 * @return
	 */
	public static String getFieldTaleId(FormTableBean table, String disPlay) {
        if(table == null) {
            return null;
        }
        FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
        if(field == null) {
            return null;
        }
        return field.getName();
    }

	public static String getFieldTaleId(FormDataBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		if(field == null) {
			return null;
		}
		return field.getName();
	}

	public static FormFieldBean getFieldBean(FormDataBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		return table.getFieldBeanByDisplay(disPlay);
	}

	public static FormFieldBean getFieldBean(FormTableBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		return bean.getFieldBeanByDisplay(disPlay);
	}


	/**
	 * Description:
	 * <pre></pre>
	 * @param bean 这里的bean必须是getMasterBean() 方法获取到的bean
	 * @param disPlay
	 * @param value
	 */
	public static void setCellValue(FormDataBean bean, String disPlay, Object value) {
		FormFieldBean cell = getFieldBean(bean, disPlay);
		if(cell != null) {
			bean.addFieldValue(cell.getName(), value);
		}
	}

	public static void setCellValue(FormDataSubBean bean, String disPlay, Object value) {
        FormFieldBean cell = getFieldBean(bean, disPlay);
        if(cell != null) {
            bean.addFieldValue(cell.getName(), value);
        }
    }

	/**
	 * Description:
	 * <pre>只适用于只有一个子表的表单</pre>
	 * @return
	 * @throws Exception
	 */
	public static List<FormDataSubBean> getSubBeans(FormDataMasterBean master) {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();
		if(null != subs && subs.size() > 0) {
			for(String key : subs.keySet()) {
				return subs.get(key);
			}
		}
		return null;
	}

	/**
	 * Description:
	 * <pre>获取从表字段</pre>
	 * @param sub
	 * @param disPlay
	 * @return
	 */
	public static Object getSubFieldValue(FormDataSubBean sub, String disPlay) {
		return getFieldValue(sub, disPlay);
	}

	public static String getSubFieldStrValue(FormDataSubBean sub, String disPlay) {
		if(null!=getFieldValue(sub, disPlay)){
			return String.valueOf(getFieldValue(sub, disPlay));
		}
		return null;
	}

	public static List<FormDataSubBean> getSubBeans(FormDataMasterBean master, String formName) throws Exception {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();
		if(null != subs && subs.size() > 0) {
			return subs.get(formName);
		}
		return null;
	}
	public static List<FormDataSubBean> getSubBeansByDisPlay(FormDataMasterBean master, String display) throws Exception {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();

		if(null != subs && subs.size() > 0) {
			for(String key : subs.keySet()) {
				if(display.equals(subs.get(key).get(0).getFormTable().getDisplay())) {
					return subs.get(key);
				}
			}
		}
		return null;
	}

	public static Map<String, List<FormDataSubBean>> getSubDataByDisPlay(FormDataMasterBean master) throws Exception {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();
		Map<String, List<FormDataSubBean>> dictMap = new HashMap<>();
		if(null != subs && subs.size() > 0) {
			for(String key : subs.keySet()) {
				dictMap.put(subs.get(key).get(0).getFormTable().getDisplay(), subs.get(key));
			}
		}
		return dictMap;
	}

	public static String getFieldStrValue(FormDataBean bean, String disPlay) {
		Object value = getFieldValue(bean, disPlay);
		return Convert.toStr(value);
	}
}
